<template>
  <div class="articleList">
    <el-card shadow="always" class=" margin_30" :body-style="{ padding: '20px' }">
      <div slot="header">
        条件查询
      </div>
      <el-form :inline="true" size="mini" :model="formData" class="demo-form-inline">
        <el-form-item label="作者">
          <el-input v-model="formData.author" placeholder="审批人" />
        </el-form-item>
        <el-form-item label="标题">
          <el-input v-model="formData.title" placeholder="审批人" />
        </el-form-item>
        <el-form-item label="编辑类型">
          <el-select v-model="formData.editorType" placeholder="请选择">
            <el-option
              v-for="item in editType"
              :key="item.id"
              :label="item.text"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card class="box-card margin_30">
      <div slot="header" class="clearfix">
        <el-button type="primary" @click="addArticle">新增</el-button>
      </div>
      <el-table
        :data="list"
        border
        style="width: 100%"
      >
        <el-table-column
          prop="title"
          label="文章标题"
        />
        <el-table-column
          prop="coverImg"
          label="展示图片"
        >
          <template v-slot="scope">
            <!-- 通过 Scoped slot 可以获取到 row, column, $index 和 store（table 内部的状态管理）的数据 -->
            <!-- {{ scope.row.coverImg }} -->
            <img class="pic" :src="scope.row.coverImg" alt="">
          </template>
        </el-table-column>
        <el-table-column
          prop="createTime"
          label="添加时间"
        />
        <el-table-column
          prop="coverImg"
          label="文章是否展示"
        >
          <template v-slot="scope">
            <!-- 通过 Scoped slot 可以获取到 row, column, $index 和 store（table 内部的状态管理）的数据 -->
            <!-- {{ scope.row.coverImg }} -->
            <el-switch
              v-model="scope.row.isShow"
              @change="uptStatus(scope.row)"
            />
          </template>
        </el-table-column>
        <el-table-column
          prop="coverImg"
          label="操作"
          fixed="right"
        >
          <template v-slot="scope">
            <!-- 通过 Scoped slot 可以获取到 row, column, $index 和 store（table 内部的状态管理）的数据 -->
            <!-- {{ scope.row.coverImg }} -->
            <el-link style="margin-right:10px" :underline="false" icon="el-icon-view" @click="goEdit(scope.row.id)">编辑文章</el-link>
            <el-link type="danger" :underline="false" @click="delArticle(scope.row.id)">删除</el-link>
          </template>
        </el-table-column>
      </el-table>
      <!--
        current-page   当前页数  start
        page-sizes     每页数据数量
        page-size      100          limit
        total          数据总数
       -->
      <el-pagination
        :current-page="start"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>

  </div>
</template>

<script>
import {
  findArticles,
  changeShowStatus,
  del
} from '@/api/contentManage/articleList'
export default {
  data() {
    return {
      list: [],
      editType: [
        {
          id: 0,
          text: '富文本'
        },
        {
          id: 1,
          text: 'markdown'
        }
      ],
      formData: {
        'author': '',
        'collectCount': '',
        'content1': '',
        'content2': '',
        'coverImg': '',
        'createTime': '',
        'editorType': '',
        'id': '',
        'isShow': '',
        'modifyTime': '',
        'summary': '',
        'title': '',
        'viewCount': '',
        'zanCount': ''
      },
      start: 1,
      limit: 10,
      total: 0
    }
  },
  computed: {

  },
  created() {
    this.init()
  },
  mounted() {

  },
  methods: {
    goEdit(id) {
      this.$router.push({ name: 'contentManagEditArticle', params: { id: id }})
    },
    addArticle() {
      this.$router.push({ name: 'contentManageAddArticle' })
    },
    onSubmit() {
      this.init()
    },
    handleCurrentChange(val) {
      console.log(val)
      this.start = val
      this.init()
    },
    handleSizeChange(val) {
      console.log(val)
      this.limit = val
      this.init()
    },
    init() {
      findArticles(this.start, this.limit, this.formData)
        .then(res => {
          console.log(res)
          res.data.rows.forEach(ele => {
            ele.isShow = !!ele.isShow
          })
          this.total = res.data.total
          this.list = res.data.rows
        })
    },
    delArticle(id) {
      del(id)
        .then(res => {
          this.$message({
            message: '文章删除成功',
            type: 'success'
          })
          this.init()
        })
    },
    uptStatus(val) {
      console.log(val)
      changeShowStatus({
        isShow: val.isShow ? 1 : 0,
        id: val.id
      })
        .then(res => {
          this.$message({
            message: '文章修改成功',
            type: 'success'
          })
        })
    }
  }
}
</script>

<style scoped lang="scss">
.articleList{

  .pic{
    width: 100px;
    height: 100px;
  }
}
</style>
